package util

/**
  * 计算经纬度点间距 单位（米）
  */

object Distance {

  private val EARTH_RADIUS = 6378.137 // 单位千米

  private def getRadian(degree: Double) = degree * Math.PI / 180.0
  //经度 纬度
  def distance(lng1: Double, lat1: Double, lng2: Double, lat2: Double): Double = {
    val radLat1 = getRadian(lat1)
    val radLat2 = getRadian(lat2)
    val a = radLat1 - radLat2
    // 两点纬度差
    val b = getRadian(lng1) - getRadian(lng2)
    // 两点的经度差
    var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)))
    s = s * EARTH_RADIUS
    s * 1000
  }
}

